package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.SetmealDao;
import com.itheima.dao.SetmealDishDao;
import com.itheima.pojo.DTO.SetmealDto;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.ISetmealDishService;
import com.itheima.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements ISetmealService {

    @Autowired
    SetmealDao setmealDao;

    @Autowired
    ISetmealDishService setmealDishService;


    //新增套餐
    @Override
    public void saveSetmealWithDish(SetmealDto setmealDto) {
        //1.先新增套餐菜品
        this.save(setmealDto);

        //2.获取到菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
    /*
    * 删除套餐
    * */
    @Override
    public void deleteSetmealWithDish(Long[] ids) {
        for (Long id : ids) {
            //1.先删除套餐
            setmealDao.deleteById(id);
            //2.查询套餐是下的菜品
            //条件：找出id是meal的id
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId,id);

            List<SetmealDish> list = setmealDishService.list(wrapper);

            //删除套餐对应的菜品
            if (!list.isEmpty()){
                ArrayList<Long> dishId = new ArrayList<>();
                list.forEach( i ->{
                    dishId.add(i.getId());
                });
                setmealDishService.removeByIds(dishId);
            }

        }
    }


    //回显数据
    @Override
    public SetmealDto selectDishWithSetmeal(Long id) {
        //1.先创建SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();

        //2.查询setmeal里的数据
        Setmeal setmeal = setmealDao.selectById(id);
        //将数据拷贝到SetmealDto对象中
        BeanUtils.copyProperties(setmeal,setmealDto);

        //3.查询套餐里的菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);

        if (setmealDishes!=null){
            setmealDto.setSetmealDishes(setmealDishes);
        }
        return setmealDto;
    }

    //修改数据
    @Override
    public void updateSetmealWithDish(SetmealDto setmealDto) {
        //1.先查询套餐里的字段
        this.updateById(setmealDto);

        //2.查看数据库是否有该套餐对应的菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);

        //3.先将数据库中的数据删除
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDishService.removeById(setmealDish);
        }

        //4.将SetmealDto对象里存放的添加到数据
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();

        for (SetmealDish dish : dishes) {
            dish.setSetmealId(setmealDto.getId());
            setmealDishService.save(dish);
        }
    }
}
